<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #login_wrap {
      width: 600px;
      margin: 20px auto;
    }
  </style>
</head>

<body>
  <div id="login_wrap">
    <h1>登录页面</h1>
    手机号：<input type="text" id="phone">
    <br>
    密码：<input type="text" id="password">
    <br>
    <div>
      验证码: <input type="text"> <canvas id="mycanvas" width="160" height="35" style="background:#ccc"></canvas>
    </div>
    <button id="loginBtn">登录</button>
  </div>



  <script src="./jquery-3.4.1.min.js"></script>
  <script src="./veri.js"></script>
  <script>
    $("#loginBtn").click(function () {

      var reg = /^1[3|4|5|6|7|8|9]\d{9}$/
      if (reg.test($("#phone").val())) {
        $.ajax({
          url: "http://127.0.0.1:3000/users/login",
          type: "post",
          data: {
            phone: $("#phone").val(),
            password: $("#password").val()
          },
          success: function (res) {
            console.log(res)
            if (res.success) {
              $("#phone").val("")
              $("#password").val("")
              // 把后台返回的token存储到localStorage 
              localStorage.token = res.token;
              alert("登录成功..正在跳转主页")
            }
            if (res.err) {
              alert(res.err)
            }
          }
        })
      } else {
        alert("手机号格式错误")
      }
    })
  </script>
</body>

</html>